package cate.game.activity.crossrank;

import cate.common.table.activity.crossrankactivity.row.CrossRankActivityBaseRow;
import cate.common.table.d.GDFunc;
import cate.game.GameBody;
import cate.game.activity.base.core.Activity;
import cate.game.activity.base.core.ActivityHandler;
import cate.game.activity.base.core.EmptyActivityGlobalData;
import cate.game.activity.crossrank.handler.CrossRankActivityLoader;
import cate.game.activity.crossrank.po.CrossRankActivity;
import cate.game.rank.cross.h.BaseCrossRankLoader;
import cate.game.role.Role;

/**
 * 跨服排行活动管理器
 */
@Activity(relatedFuncIds = {
        GDFunc.Help.CROSS_SPICE_CALL_RANK,
        GDFunc.Help.CROSS_WISH_CRYSTALL_RANK,
        GDFunc.Help.CROSS_DIAMOND_RANK,
        GDFunc.Help.CROSS_GOD_EQUIP_RANK
})
public class SingleCrossRankActivityHandler extends ActivityHandler<EmptyActivityGlobalData, CrossRankActivity> {
    @Override
    public void initialize(GameBody game) throws Exception {
        super.initialize(game);
        createActivityHandler();
    }

    @Override
    public String getName() {
        return "跨服排行活动";
    }

    @Override
    public EmptyActivityGlobalData buildGlobal() {
        return EmptyActivityGlobalData.BUILD();
    }

    @Override
    public CrossRankActivity buildSingle() {
        return new CrossRankActivity();
    }

    @Override
    public void onActOpen() {
        initCrossRankHandler();
    }

    @Override
    public void onActClose() {

    }

    /**
     * 初始化跨服排行处理器
     */
    private void initCrossRankHandler(){
        createActivityHandler();
    }

    /**
     * 创建跨服排行处理器
     */
    private void createActivityHandler(){
        CrossRankActivityBaseRow baseRow = game.table.crossRankActivity.base.get(configTid());
        if(baseRow == null){
            return;
        }
        if(!game.crossRank.existCrossRankHandler(baseRow.rankId)){
            CrossRankActivityLoader loader = new CrossRankActivityLoader(game, baseRow.rankId, configTid());
            game.crossRank.registerRankHandler(loader);
        }
    }

    /**
     * 排行值记录
     *
     * @param role 角色
     * @param event 事件
     * @param param 参数
     * @param value 值
     * @param add 是否添加
     */
    public void doRecord(Role role, int event, int param, long value, boolean add){
        CrossRankActivityBaseRow baseRow = game.table.crossRankActivity.base.get(configTid());
        if(baseRow == null){
            return;
        }
        if(baseRow.event == event && baseRow.param == param){
            CrossRankActivity activity = role.getActivity().getEnableActivitySingle(baseRow.funcId);
            if(activity != null){
                long newValue = activity.record(value, add);
                activity.initSend();
                BaseCrossRankLoader handler = game.crossRank.getHandler(baseRow.rankId);
                if(handler != null && newValue >= baseRow.condition){
                    try{
                        handler.record(role, newValue, true);
                    } catch (Exception e) {
                        log.error("同步跨服排行榜数据出错, e=", e);
                    }
                }
            }
        }
    }
}
